package controller;

import java.io.IOException;
import java.net.URL;
import java.rmi.dgc.Lease;
import java.util.ResourceBundle;

import com.sun.org.apache.xml.internal.security.utils.resolver.implementations.ResolverDirectHTTP;
import com.sun.xml.internal.bind.v2.model.core.ID;

import demo.Main;
import demo.Session;
import demo.entity.User;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.Node;
import javafx.scene.control.Accordion;

import javafx.scene.control.MenuBar;

import javafx.scene.control.TitledPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Main2Controller  implements Initializable, EventHandler<ActionEvent>{
	@FXML
	private MenuBar menubar;
	@FXML
	private Accordion accordion;
	@FXML
	private TitledPane tPanegoods;
	@FXML
	private Button btnInput;
	@FXML
	private Button btnShowGoods;
	@FXML
	private Button btnStore;
	@FXML
	private TitledPane paneOrder;
	@FXML
	private Button btnSubmitOrder;
	@FXML
	private Button btnSubmit;
	@FXML
	private Button btnShowOrder;
	@FXML
	private Button btnPayOrder;
	@FXML
	private Button btnReport;
	@FXML
	private Button btnRePay;
	@FXML
	private Button btnMain;
	@FXML
	private Button btn41;
	@FXML
	private Button btnModify;
	@FXML
	private Button btn43;
	@FXML
	private Pane mainPane;
    @FXML
    private ImageView imgTop;
    @FXML
    private ImageView imgMainTop;
    @FXML
    private ImageView imgMainStage;
    @FXML
    private Button exit;
    
    private Main main;
    private User user = Session.getSession().getCurrentUser();
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		if(user.getRole() == 0) {
			paneOrder.setDisable(true);
			btnSubmit.setDisable(true);
			btnRePay.setDisable(true);
		}
		else if(user.getRole() == 1) tPanegoods.setDisable(true);
		
		btnSubmitOrder.setOnAction(this);
		btnShowOrder.setOnAction(this);
		btnShowGoods.setOnAction(this);
		btnPayOrder.setOnAction(this);
		btnModify.setOnAction(this);
		btnSubmit.setOnAction(this);
		btnReport.setOnAction(this);
		btnInput.setOnAction(this);
		btnRePay.setOnAction(this);
		btnMain.setOnAction(this);
		exit.setOnAction(this);
	}

	public void setApp(Main main) {
		this.main = main;
	}
	
	private void switchView(String fxml){
		ObservableList<Node> paneCildren = mainPane.getChildren();
		//System.out.println(fxml);
		try {
			paneCildren.clear();
			paneCildren.add(FXMLLoader.load(getClass().getResource(fxml)));
		}catch(IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void handle(ActionEvent event) {
		// TODO Auto-generated method stub
		if(event.getSource() == btnSubmitOrder || event.getSource() == btnSubmit) switchView("/demo/Order.fxml");
		else if(event.getSource() == btnShowOrder || event.getSource() == btnRePay) switchView("/demo/Pay.fxml");
		else if(event.getSource() == btnReport) switchView("/demo/Report.fxml");
		else if(event.getSource() == btnPayOrder) switchView("/demo/PayDetail.fxml");
		else if(event.getSource() == btnInput) switchView("/demo/Input.fxml");
		else if(event.getSource() == btnShowGoods) switchView("/demo/GoodsMagement.fxml");
		else if(event.getSource() == btnModify) switchView("/demo/Modify.fxml");
		else if(event.getSource() == exit) main.gotoLogin();
		else if(event.getSource() == btnMain ) {
			mainPane.getChildren().clear();
			mainPane.getChildren().add(imgMainStage);
		}
	}
}